﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace IngredSubTool
{
    /// <summary>
    /// Handles retrieving the ingredient/substitution information from the flat storage file and converting it to the relevant data classes to be used later.
    /// </summary>
    public class DataLoader
    {
        public static List<SearchCategory> SearchCategories { get; set; }
        public static List<Substitution> Substitutions { get; set; }
        public static List<Comment> Comments { get; set; }
        public static List<UnitsOfMeasure> Units { get; set; }

        public static void LoadCategories()
        {
            SearchCategories = new List<SearchCategory>();

            StreamReader reader = new StreamReader("Resources\\categories.txt");
            try
            {
                while (reader.Peek() != -1)
                {
                    String catLine = reader.ReadLine();
                    if (!catLine.StartsWith("#"))
                    {
                        SearchCategories.Add(new SearchCategory(catLine));
                    }
                }
            }
            catch (Exception e)
            {
                //TODO Text IO Bad. How to handle this?
                MessageBox.Show(e.Message);
            }
            finally
            {
                reader.Close();
            }
        }

        public static void LoadSubstitutions()
        {
            Substitutions = new List<Substitution>();

            StreamReader reader = new StreamReader("Resources\\substitutions.txt");
            try
            {
                while (reader.Peek() != -1)
                {
                    String subLine = reader.ReadLine();
                    if (!subLine.StartsWith("#"))
                    {
                        String[] vals = subLine.Split(';');
                        Substitutions.Add(new Substitution(vals[0], vals[1], vals[2].Replace('~', '\n'), vals[3], vals[4]));
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("LoadSub "+e.Message);
            }
            finally
            {
                reader.Close();
            }
        }


        public static void LoadComments()
        {

            StreamReader reader = new StreamReader("Resources\\comments.txt");
            try
            {
                while (reader.Peek() != -1)
                {
                    String subLine = reader.ReadLine();
                    if (!subLine.StartsWith("#"))
                    {
                        String[] vals = subLine.Split(';');
                        int rating = int.Parse(vals[4]);

                        DateTime date = new DateTime();
                        try
                        {
                            date = DateTime.ParseExact(vals[5],"dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Error trying to parse date/time from file: " + vals[5] + " Expected format: dd/MM/yyyy HH:mm");
                        }

                        Comment com = new Comment(vals[0], vals[1], vals[2], vals[3], rating, date);

                        //Get the related substitution.
                        foreach (Substitution sub in Substitutions)
                        {
                            if (sub.Original.Equals(vals[0]) && sub.Replacement.Equals(vals[1]))
                            {
                                sub.Comments.Add(com);
                            }
                        }

                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("LoadComment "+e.Message);
            }
            finally
            {
                reader.Close();
            }
        }

        public static void LoadUnits()
        {
            Units = new List<UnitsOfMeasure>();


            StreamReader reader = new StreamReader("Resources\\units.txt");
            try
            {
                while (reader.Peek() != -1)
                {
                    String unitLine = reader.ReadLine();
                    if (!unitLine.StartsWith("#"))
                    {
                        String[] vals = unitLine.Split(';');
                        int temp = 0;
                        if (int.TryParse(vals[1], out temp))
                        {
                            Units.Add(new UnitsOfMeasure(vals[0], temp));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("LoadUnits "+e.Message);
            }
            finally
            {
                reader.Close();
            }
        }
    }
}
